﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.ShiftEdit
{
    public partial class shiftEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button b = (Button)sender;

                if (b.Text.Equals("Remove"))
                {
                    deleteShift(sender, e);
                    return;
                }
            }

            string shiftToChange = (string)Session["shiftToChange"];
            this.dateLabel.Text = shiftToChange;
            deleteDiv.Visible = false;
            editDiv.Visible = false;
            addDiv.Visible = false;
        }

        protected void delShiftFunction(object sender, EventArgs e)
        {
            deleteDiv.Visible = true;
            editDiv.Visible = false;
            addDiv.Visible = false;
            Label2.Text = "";           
        }

        //protected override void OnInit(EventArgs e)
        //{

        //    LoginDetails details = (LoginDetails)Session["loginDetails"];
        //    string shiftToChange = (string)Session["shiftToChange"];
        //    LinkedList<Shift> shifts = BL.getAllShiftsByIdAndDate(details.ID, shiftToChange);

        //    foreach (Shift shift in shifts)
        //    {
        //        Label label = new Label();

        //        label.Text = shift.getStartHour() + ":" + shift.getStartMin() + "-" + shift.getEndHour() + ":" + shift.getEndMin();

        //        LinkButton button = new LinkButton();

        //        button.Text = "Remove";
        //        button.ID = shift.getStartHour() + ":" + shift.getStartMin();
        //        button.Attributes.Add("runat", "server");

        //        // button.Attributes.Add("OnClick", "deleteShift");
        //        //button.Attributes.Add("onClick", "deleteShift");
        //        //button.Attributes.Add("CausesValidation","false");
        //        //button.OnClientClick = "deleteShift";
        //        //button.CausesValidation = false;
        //        //button.Attributes.Add("type", "button");
        //        //this.ph1.Controls.Add(label);
        //        this.ph1.Controls.Add(button);
        //        button.Click += new EventHandler(deleteShift);
        //    }
        //}

        protected void editShiftFunction(object sender, EventArgs e)
        {
            deleteDiv.Visible = false;
            editDiv.Visible = true;
            addDiv.Visible = false;
            Label2.Text = "";


        }

        protected void addNewShiftFunction(object sender, EventArgs e)
        {
            deleteDiv.Visible = false;
            editDiv.Visible = false;
            addDiv.Visible = true;
            Label2.Text = "";
        } 

        protected void addShift(object sender, EventArgs e)
        {
            try
            {
                LoginDetails details = (LoginDetails)Session["loginDetails"];
                string shiftToChange = (string)Session["shiftToChange"];

                string from = shiftToChange + "::" + startTime.Text.Trim();
                string to = null;
                if (endTime.Text.Trim().Length > 0) { to = shiftToChange + "::" + endTime.Text.Trim(); }
                if (BL.addNewShift(details.ID, from, to))
                {
                    Label2.Text = "Shift Add Successfully";
                }
                else
                {
                    Label2.Text = "faild to add shift <br/> curect format is HH:MM";
                }
            }
            catch
            {

            }
        }

        protected void deleteShift(object sender, EventArgs e)
        {
            try
            {
                Button clickedButton = (Button)sender;

                LoginDetails details = (LoginDetails)Session["loginDetails"];
                string shiftToChange = (string)Session["shiftToChange"];
                LinkedList<Shift> shifts = BL.getAllShiftsByIdAndDate(details.ID, shiftToChange);
            }
            catch { }
        }
            
        protected void endTime_TextChanged(object sender, EventArgs e)
        {

        }
    }
}